package com.sbcb.common.applicationcontext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

public class SpringUtil {
    private static ApplicationContext applicationContext = null;
    private static Environment environment = null;

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
        if(SpringUtil.applicationContext==null){
            SpringUtil.applicationContext = applicationContext;
            environment = applicationContext.getEnvironment();
        }
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    public static Environment getEnvironment(){
        return environment;
    }

    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name,clazz);
    }

    public static String getString(String key){
        return environment.getProperty(key);
    }
}
